<?php
/**
 * Created by PhpStorm.
 * User: Leonly
 * Date: 2015/10/14 0014
 * Time: 9:57
 */

class OrderModel extends Model{
    protected $tableName = 'item_orders';
    /**
     * 计算用户指定时间范围内的汽车膜/建筑膜返利
     * @param $times
     * @param $timee
     * @return array
     */
    public function getUserRebate($uid, $times, $timee) {
        $map = array(
            'uid' => $uid,
            'status' => 4,
            'type'  => 1,
            'cate'  => '汽车膜',
            'done_time' => array('between', "$times, $timee" )
        );


        $result = array();

        $c_result = $this->where($map)->field('sum(`count`) as c, sum(zhifue) as money')->find();
        if(empty($c_result['c'])) {
            $result[1] = array('award'=>0, 'count' => 0, 'money' => 0);
        } else {
            $result[1] = array(
                'award' => rebate(1, $c_result['c'], $c_result['money']),
                'count' =>  $c_result['c'],
                'money' =>  $c_result['money']
            );
        }

        $map['cate'] = '建筑膜';
        $p_result = $this->where($map)->field('sum(`count`) as c, sum(zhifue) as money')->find();
        if(empty($p_result['c'])) {
            $result[2] = array('award'=>0, 'count' => 0, 'money' => 0);
        } else {
            $result[2] = array(
                'award' => rebate(1, $p_result['c'], $p_result['money']),
                'count' =>  $p_result['c'],
                'money' =>  $p_result['money']
            );
        }

        return $result;
    }


    /**
     * 计算用户本月汽车膜/建筑膜返利
     * @param $uid
     * @return array
     */
    public function getUserRebateMonth($uid) {
        $st =date('Y-m-01', strtotime(date("Y-m-d")));
        $et = date('Y-m-d', strtotime("$st +1 month -1 day"));
        return $this->getUserRebate($uid, $st, $et);
    }
}